package com.hp.attendance.dao;

import com.hp.attendance.entity.Attendance;
import com.hp.attendance.entity.Department;
import com.hp.attendance.entity.Role;
import com.hp.attendance.util.BaseDAO;

import java.io.IOException;
import java.sql.SQLException;
import java.util.List;

public class AttendanceDao extends BaseDAO {

    public void addStartAttend(Attendance att) throws SQLException, IOException, ClassNotFoundException {
        String sql = "insert into attendance (id,employee_number,day,time_type,start_time,start_type) values (null,?,?,?,?,?)";
        Object[] obj = {att.getEmployee_number(), att.getDay(), att.getTime_type(), att.getStart_time(), att.getStart_type()};
        addOrUpdOrDel(sql, obj);
    }

    public void addEndAttend(Attendance att) throws SQLException, IOException, ClassNotFoundException {
        String sql = "update attendance set end_time=?,end_type=?,work_type=?,notes=? where employee_number=?";
        Object[] obj = {att.getEnd_time(), att.getEnd_type(), att.getWork_type(), att.getNotes(), att.getEmployee_number()};
        addOrUpdOrDel(sql, obj);

    }

    public List<Attendance> queryAttList() throws ClassNotFoundException, SQLException, InstantiationException, IllegalAccessException, IOException {
        String sql="select * from attendance";
        List<Attendance> list = queryList(sql, new Object[0], new Attendance());
        return list;


    }

        public List<Attendance> attendance(int employee_number) throws SQLException, IOException, ClassNotFoundException, InstantiationException, IllegalAccessException {
            String sql ="select * from attendance where employee_number=?";
            Object[] obj={employee_number};
            List<Attendance> att = queryList(sql, obj, new Attendance());
            return att;
        }
    /**
     * 分页查询记录
     * @param pageNo 当前页
     * @param pageSize 每页显示的条数
     * @return
     */
    public List<Attendance> getLimitList(int employee_number,int pageNo, int pageSize) throws SQLException, IOException, ClassNotFoundException, InstantiationException, IllegalAccessException {
        String sql ="select * from attendance where employee_number=? limit ?,?";
        Object[] obj ={employee_number,(pageNo-1)*pageSize,pageSize};
        List<Attendance> menus = queryList(sql, obj, new Attendance());
        return menus;
    }

    /**
     * 查询总记录数
     * @return
     * @throws SQLException
     */
    public long getTotal(int employee_number) throws SQLException, IOException, ClassNotFoundException, InstantiationException, IllegalAccessException {
        String sql ="select count(1) from attendance where employee_number=?";
        Object[] obj ={employee_number};
        int count = queryCount(sql, obj);
        return count;
    }

    public Attendance queryAttByNumDay(int empNum,String day) throws ClassNotFoundException, SQLException, InstantiationException, IllegalAccessException, IOException {
        String sql="select * from attendance where employee_number=? and day=?";
        Object[] obj={empNum,day};
        Attendance a = queryOne(sql, obj, new Attendance());
        return a;

    }
    public void updateAtt(int empNum,String day) throws SQLException, IOException, ClassNotFoundException {
        String sql="update attendance set start_type='正常' where employee_number=? and day=?";
        Object[] obj={empNum,day};
        addOrUpdOrDel(sql,obj);

    }

}
